#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        queue<TreeNode*> q;
        queue<unsigned int> qi;
        q.push(root);
        qi.push(1);
        unsigned int ret = 0;
        while (q.size())
        {
            unsigned int left = 0, right = 0;
            int n = q.size();
            for (int i = 0; i < n; i++)
            {
                TreeNode* tn = q.front();
                unsigned int ti = qi.front();
                if (i == 0) left = ti;
                if (i == n - 1) right = ti;
                if (tn->left) q.push(tn->left), qi.push(ti * 2);
                if (tn->right) q.push(tn->right), qi.push(ti * 2 + 1);
                q.pop(), qi.pop();
            }
            ret = max(ret, right - left + 1);
            cout << left << ' ' << right << endl;;
        }
        return ret;
    }
};